package com.springBoot.springBootSysCore.modules.controller.web.base;

import com.springBoot.springBootSysCore.common.utils.FileUtil;
import com.springBoot.springBootSysCore.common.utils.ObjectUtils;
import com.springBoot.springBootSysCore.common.utils.json.JsonUtil;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import com.springBoot.springBootSysCore.modules.services.system.SysFileTmpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("fileTmp")
public class FileTmpController {
	@Autowired
	private SysFileTmpService fileTmpService;

	@RequestMapping("simpleUploadFile")
	@ResponseBody
	public Object simpleUploadFile(HttpServletRequest request) throws JsonProcessingException {
		ResultJsonFormat rj = new  ResultJsonFormat( CodeEnum.SUCCESS.value());
		Map<String, String> m = FileUtil.upLoadFile("userfiles/user", request);
		if(StringUtils.isNotBlank(m.get("file"))){
			rj.setData(m.get("file"));
		}
		return rj.convertResultJsonForData();
	}
	 /**
     * 上传文件id
     * 返回缓存对象
     * @param fileId
     * @param tmpId
	 * @throws JsonProcessingException 
     */
    @RequestMapping("uploadFileId")
    @ResponseBody
    public String uploadFileId(String fileId,String tmpId) throws JsonProcessingException {
    	return	JsonUtil.writeValueAsString(fileTmpService.uploadFileId(fileId,tmpId));
    }
    /**
     * 上传文件-文件缓存对象
     * @param file
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("saveFile")
	@ResponseBody
	public String saveFile(@RequestParam(value="file",required=true)MultipartFile file,String objId) throws JsonProcessingException{
		return JsonUtil.writeValueAsString(fileTmpService.uploadFile(file,objId));
	}
    /**
     * 根据文件id和自定义对象id删除缓存对象以及资源
     * @param fileId
     * @param objId
     * @return
     * @throws JsonProcessingException 
     */
    @RequestMapping("deleteFile")
	@ResponseBody
    public String deleteFile(String fileId,String objId) throws JsonProcessingException{
    	return JsonUtil.writeValueAsString(fileTmpService.deleteFile(fileId,objId));
    }
	/**
	 * 上传文件
	 * @param files
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("saveFiles")
	@ResponseBody
	public String saveFiles(@RequestParam(value="fileupload",required=true)MultipartFile[] files,String username,String clientDigest) throws JsonProcessingException{
		return JsonUtil.writeValueAsString(fileTmpService.uploadFile(files,username,clientDigest));
	}
	@RequestMapping("fileupload")
	@ResponseBody
	public String fileupload(HttpServletRequest httpServletRequest, String username, String clientDigest) throws JsonProcessingException{
		return JsonUtil.writeValueAsString(fileTmpService.fileupload(httpServletRequest,username,clientDigest));
	}
}
